Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: environment handling windows (host mode) #1732

Merged
merged 11 commits into from
Apr 18, 2023

Conversation

ChristopherHX
Copy link
Contributor

@ChristopherHX ChristopherHX commented Apr 16, 2023

Why?

Currently we merge env variables with case sensitive key, this is problematic on windows where PATH and Path are the same key.

Other Changes considered:
Create a custom env type

type Map[K comparable, V any] interface {
	Set(key K, val V)
	Get(key K) V
	TryGet(key K) (V, bool)
	AsNative() map[K]V
}

type CaseInsensitiveMap[V any] struct {
	lookUp map[string]string
	Native map[string]V
}

func (m *CaseInsensitiveMap[V]) getKey(key string) string {
	return strings.Map(func(r rune) rune {
		for {
			next := unicode.SimpleFold(r)
			if next <= r {
				return r
			}
			r = next
		}
	}, key)
}

func (m *CaseInsensitiveMap[V]) Set(key string, val V) {
	foldkey := m.getKey(key)
	if m.Native == nil {
		m.Native = map[string]V{}
	}
	if fk, ok := m.lookUp[foldkey]; ok {
		m.Native[fk] = val
	} else {
		if m.lookUp == nil {
			m.lookUp = map[string]string{}
		}
		m.lookUp[foldkey] = key
		m.Native[key] = val
	}
}

func (m *CaseInsensitiveMap[V]) Get(key string) V {
	v, _ := m.TryGet(key)
	return v
}

func (m *CaseInsensitiveMap[V]) TryGet(key string) (V, bool) {
	foldkey := m.getKey(key)
	if m.Native == nil {
		m.Native = map[string]V{}
	}
	if fk, ok := m.lookUp[foldkey]; ok {
		return m.Native[fk], true
	}
	return m.Native[key], false
}

func (m *CaseInsensitiveMap[V]) AsNative() map[string]V {
	return m.Native
}

type RegularMap[K comparable, V any] struct {
	Native map[K]V
}

func (m *RegularMap[K, V]) Set(key K, val V) {
	if m.Native == nil {
		m.Native = map[K]V{}
	}
	m.Native[key] = val
}

func (m *RegularMap[K, V]) Get(key K) V {
	return m.Native[key]
}

func (m *RegularMap[K, V]) TryGet(key K) (V, bool) {
	v, ok := m.Native[key]
	return v, ok
}

func (m *RegularMap[K, V]) AsNative() map[K]V {
	return m.Native
}

However this would require a lot of changes across the codebase.

Next steps:

  • make the env expressions context case sensitive again for non windows, because the env keys can be duplucated in the map

@ChristopherHX ChristopherHX requested a review from a team as a code owner April 16, 2023 16:02
@ChristopherHX ChristopherHX marked this pull request as draft April 16, 2023 16:04
@pull-request-size pull-request-size bot added size/L and removed size/M labels Apr 16, 2023
@pull-request-size pull-request-size bot added size/M and removed size/L labels Apr 16, 2023
@codecov
Copy link

codecov bot commented Apr 16, 2023

Codecov Report

Merging #1732 (ab1f70d) into master (4989f44) will increase coverage by 1.07%.
The diff coverage is 66.12%.

@@            Coverage Diff             @@
##           master    #1732      +/-   ##
==========================================
+ Coverage   61.22%   62.30%   +1.07%     
==========================================
  Files          46       48       +2     
  Lines        7141     7579     +438     
==========================================
+ Hits         4372     4722     +350     
- Misses       2462     2527      +65     
- Partials      307      330      +23     
Impacted Files Coverage Δ
pkg/common/outbound_ip.go 0.00% <0.00%> (ø)
pkg/container/docker_cli.go 82.23% <ø> (ø)
pkg/container/docker_logger.go 52.08% <ø> (ø)
pkg/container/docker_pull.go 33.33% <ø> (ø)
pkg/container/docker_run.go 13.99% <0.00%> (+0.40%) ⬆️
pkg/container/docker_volume.go 0.00% <ø> (ø)
pkg/container/file_collector.go 37.30% <0.00%> (ø)
pkg/container/host_environment.go 0.00% <0.00%> (ø)
...ontainer/linux_container_environment_extensions.go 23.07% <0.00%> (-1.25%) ⬇️
pkg/exprparser/functions.go 66.32% <0.00%> (-1.04%) ⬇️
... and 22 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@pull-request-size pull-request-size bot added size/L and removed size/M labels Apr 16, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Apr 16, 2023

🦙 MegaLinter status: ✅ SUCCESS

Descriptor Linter Files Fixed Errors Elapsed time
✅ EDITORCONFIG editorconfig-checker 9 0 0.05s
✅ REPOSITORY gitleaks yes no 2.53s
✅ REPOSITORY git_diff yes no 0.0s
✅ REPOSITORY secretlint yes no 1.26s

See detailed report in MegaLinter reports
Set VALIDATE_ALL_CODEBASE: true in mega-linter.yml to validate all sources, not only the diff

MegaLinter is graciously provided by OX Security

@ChristopherHX ChristopherHX marked this pull request as ready for review April 16, 2023 18:12
pkg/runner/step.go Outdated Show resolved Hide resolved
pkg/runner/step.go Outdated Show resolved Hide resolved
@mergify mergify bot requested a review from a team April 17, 2023 09:57
ChristopherHX and others added 2 commits April 17, 2023 12:45
@mergify
Copy link
Contributor

mergify bot commented Apr 18, 2023

@ChristopherHX this pull request has failed checks 🛠

@mergify mergify bot added needs-work Extra attention is needed and removed needs-work Extra attention is needed labels Apr 18, 2023
@mergify mergify bot merged commit 9884da0 into master Apr 18, 2023
@mergify mergify bot deleted the fix-env-handling-windows-host-mode branch April 18, 2023 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants